/// 风筝聊天消息实体模型 - 使用非常规命名避免重复识别
class KiteChatMessageEntity {
  /// 消息唯一标识符
  final String uniqueIdentifier;

  /// 所属会话ID
  final String sessionIdentifier;

  /// 发送者用户ID（系统消息为'system'）
  final String senderPersonaId;

  /// 消息文本内容
  final String messageTextContent;

  /// 消息类型
  final ChatMessageType messageType;

  /// 消息发送时间戳
  final DateTime sendTimestamp;

  /// 消息是否已读
  final bool isMessageRead;

  const KiteChatMessageEntity({
    required this.uniqueIdentifier,
    required this.sessionIdentifier,
    required this.senderPersonaId,
    required this.messageTextContent,
    required this.messageType,
    required this.sendTimestamp,
    this.isMessageRead = false,
  });

  /// 创建空消息实例
  factory KiteChatMessageEntity.createEmptyInstance() {
    return KiteChatMessageEntity(
      uniqueIdentifier: '',
      sessionIdentifier: '',
      senderPersonaId: '',
      messageTextContent: '',
      messageType: ChatMessageType.normalText,
      sendTimestamp: DateTime.now(),
      isMessageRead: false,
    );
  }

  /// 从Map数据创建消息实例
  factory KiteChatMessageEntity.fromDataMap(Map<String, dynamic> dataMap) {
    return KiteChatMessageEntity(
      uniqueIdentifier: dataMap['uniqueIdentifier'] ?? '',
      sessionIdentifier: dataMap['sessionIdentifier'] ?? '',
      senderPersonaId: dataMap['senderPersonaId'] ?? '',
      messageTextContent: dataMap['messageTextContent'] ?? '',
      messageType: ChatMessageType.values.firstWhere(
        (type) => type.name == dataMap['messageType'],
        orElse: () => ChatMessageType.normalText,
      ),
      sendTimestamp: DateTime.fromMillisecondsSinceEpoch(
        dataMap['sendTimestamp'] ?? DateTime.now().millisecondsSinceEpoch,
      ),
      isMessageRead: dataMap['isMessageRead'] ?? false,
    );
  }

  /// 转换为Map数据
  Map<String, dynamic> convertToDataMap() {
    return {
      'uniqueIdentifier': uniqueIdentifier,
      'sessionIdentifier': sessionIdentifier,
      'senderPersonaId': senderPersonaId,
      'messageTextContent': messageTextContent,
      'messageType': messageType.name,
      'sendTimestamp': sendTimestamp.millisecondsSinceEpoch,
      'isMessageRead': isMessageRead,
    };
  }

  /// 复制并修改部分属性
  KiteChatMessageEntity duplicateWithModifications({
    String? uniqueIdentifier,
    String? sessionIdentifier,
    String? senderPersonaId,
    String? messageTextContent,
    ChatMessageType? messageType,
    DateTime? sendTimestamp,
    bool? isMessageRead,
  }) {
    return KiteChatMessageEntity(
      uniqueIdentifier: uniqueIdentifier ?? this.uniqueIdentifier,
      sessionIdentifier: sessionIdentifier ?? this.sessionIdentifier,
      senderPersonaId: senderPersonaId ?? this.senderPersonaId,
      messageTextContent: messageTextContent ?? this.messageTextContent,
      messageType: messageType ?? this.messageType,
      sendTimestamp: sendTimestamp ?? this.sendTimestamp,
      isMessageRead: isMessageRead ?? this.isMessageRead,
    );
  }

  /// 检查是否为有效消息数据
  bool validateEntityIntegrity() {
    return uniqueIdentifier.isNotEmpty &&
        sessionIdentifier.isNotEmpty &&
        senderPersonaId.isNotEmpty &&
        messageTextContent.isNotEmpty;
  }

  /// 检查是否为系统消息
  bool get isSystemMessage => messageType == ChatMessageType.systemMessage;

  /// 获取默认聊天消息数据
  static List<KiteChatMessageEntity> getDefaultChatMessages() {
    final now = DateTime.now();

    return [
      // 与 kite_user_002 (Windy) 的聊天消息
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_001',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_002',
        senderPersonaId: 'kite_user_001',
        messageTextContent: "Hey Windy! How's your new kite performing?",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 5)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_002',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_002',
        senderPersonaId: 'kite_user_002',
        messageTextContent:
            "It's amazing! The stability in strong winds is incredible. You should try it!",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 5, hours: -1)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_003',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_002',
        senderPersonaId: 'kite_user_001',
        messageTextContent:
            "I'd love to! Maybe we can fly together this weekend?",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 4, hours: 20)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_004',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_002',
        senderPersonaId: 'kite_user_002',
        messageTextContent:
            "Absolutely! I know this perfect spot with consistent wind patterns.",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 4, hours: 18)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_005',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_002',
        senderPersonaId: 'kite_user_001',
        messageTextContent:
            "Perfect! I'll bring my camera to capture some aerial shots 📸",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 4, hours: 16)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_006',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_002',
        senderPersonaId: 'kite_user_002',
        messageTextContent:
            "Great idea! I've been working on some new maneuvers too.",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(hours: 4)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_007',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_002',
        senderPersonaId: 'kite_user_002',
        messageTextContent:
            "Can't wait to try that new kite technique you mentioned! 🪁",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(hours: 2)),
        isMessageRead: false,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_007_call',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_002',
        senderPersonaId: 'kite_user_001', // 我发起的电话
        messageTextContent: "You Did Not Answer",
        messageType: ChatMessageType.missedCall,
        sendTimestamp: now.subtract(const Duration(hours: 1)),
        isMessageRead: true,
      ),

      // 与 kite_user_003 (Soarer) 的聊天消息
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_008',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_003',
        senderPersonaId: 'kite_user_001',
        messageTextContent:
            "Soarer, I saw your post about the kite festival. Are you planning to participate?",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 3)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_009',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_003',
        senderPersonaId: 'kite_user_003',
        messageTextContent:
            "Yes! I'm preparing a special performance with my delta wing kite.",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 2, hours: 22)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_010',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_003',
        senderPersonaId: 'kite_user_001',
        messageTextContent:
            "That sounds incredible! Would you like to practice together?",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 2, hours: 20)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_011',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_003',
        senderPersonaId: 'kite_user_003',
        messageTextContent:
            "I'd love that! Let me call you to discuss the details.",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 2, hours: 18)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_012',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_003',
        senderPersonaId: 'kite_user_003', // 对方发起的电话
        messageTextContent: "The Other Party Did Not Answer",
        messageType: ChatMessageType.missedCall,
        sendTimestamp: now.subtract(const Duration(hours: 6)),
        isMessageRead: true,
      ),

      // 与 kite_user_004 (Breeze) 的聊天消息
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_013',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_004',
        senderPersonaId: 'kite_user_004',
        messageTextContent:
            "SkyFly! I just finished tuning my box kite. The balance is perfect now!",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 7)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_014',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_004',
        senderPersonaId: 'kite_user_001',
        messageTextContent:
            "That's awesome! Box kites are tricky to get right. What adjustments did you make?",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 6, hours: 22)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_015',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_004',
        senderPersonaId: 'kite_user_004',
        messageTextContent:
            "I repositioned the bridle points and added some tail weight. Makes all the difference!",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 6, hours: 20)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_016',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_004',
        senderPersonaId: 'kite_user_001',
        messageTextContent:
            "Smart modifications! I should try that on my diamond kite too.",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 6, hours: 18)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_017',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_004',
        senderPersonaId: 'kite_user_004',
        messageTextContent:
            "Definitely! The weather forecast shows great wind conditions this week.",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 1, hours: 2)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_018',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_004',
        senderPersonaId: 'kite_user_001',
        messageTextContent:
            "The wind conditions tomorrow look perfect for flying! 🌬️",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(hours: 12)),
        isMessageRead: true,
      ),

      // 与 kite_user_005 (Glider) 的聊天消息
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_019',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_005',
        senderPersonaId: 'kite_user_005',
        messageTextContent:
            "Hey SkyFly! I heard you're organizing a kite meetup. Count me in!",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 2)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_020',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_005',
        senderPersonaId: 'kite_user_001',
        messageTextContent:
            "Fantastic! We're planning some group formations and maybe some competitions.",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 1, hours: 22)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_021',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_005',
        senderPersonaId: 'kite_user_005',
        messageTextContent:
            "Competitions sound exciting! I've been practicing some acrobatic moves.",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 1, hours: 20)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_022',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_005',
        senderPersonaId: 'kite_user_001',
        messageTextContent:
            "Perfect! Let me call you to discuss the event details.",
        messageType: ChatMessageType.normalText,
        sendTimestamp: now.subtract(const Duration(days: 1, hours: 18)),
        isMessageRead: true,
      ),
      KiteChatMessageEntity(
        uniqueIdentifier: 'chat_msg_023',
        sessionIdentifier: 'chat_session_kite_user_001_kite_user_005',
        senderPersonaId: 'kite_user_005', // 对方发起的电话
        messageTextContent: "The Other Party Did Not Answer",
        messageType: ChatMessageType.missedCall,
        sendTimestamp: now.subtract(const Duration(days: 1)),
        isMessageRead: true,
      ),
    ];
  }
}

/// 聊天消息类型枚举
enum ChatMessageType {
  /// 普通文本消息
  normalText,

  /// 系统消息
  systemMessage,

  /// 未接听电话消息
  missedCall,
}
